<?php
class Admin_PresseController extends My_Controller_Admin_Action
{
	protected $presseModel = '';

	protected function _init()
	{
		$this->presseModel = new Admin_Model_Presse();
	}

	public function indexAction()
	{
		$rowPerPage = $this->_getParam('rowperpage', 20);
		$currentPage = 1;
		$page = $this->_getParam('page', 1);
		if(!empty($page)) {
			$currentPage = $page;
		}

		$rows = $this->presseModel->getListPresse();
		$paginator = Zend_Paginator::factory($rows);
		$paginator->setItemCountPerPage($rowPerPage);
		$paginator->setCurrentPageNumber($currentPage);

		$this->view->page = $page;
		$this->view->paginator = $paginator;
	}

	public function editAction()
	{
	    $id = $this->_getParam('id', null);
        $data = array();
        if ($id) {
            $model = new Admin_Model_Presse();
            $data = $model->getPresse($id);
        }
        $this->view->object = new My_Object($data);
	}

	public function addPresseAction()
	{
		$this->_forward('edit');
	}

	public function saveAction()
	{
	    $data = $this->_getParam('data');
	    $id = $this->_getParam('id', null);
	    $conserve = $this->_getParam('conserve');
	    $error = false;
	    $data['dtpubli'] = My_Date::fomatDbDate($data['dtpubli']);

	    if ($conserve) {
	        if (isset($conserve['fichier']) && $conserve['fichier'] != 1) {
	            Admin_Model_Upload::remove($conserve['fichier']);
	            $data['fichier'] = '';
	        }
	        if (isset($conserve['vignette']) && $conserve['vignette'] != 1) {
	            Admin_Model_Upload::remove($conserve['vignette']);
	            $data['vignette'] = '';
	        }
	    }

	    if ($_FILES['vignette']['tmp_name']) {
	        try {
    	        $result = Admin_Model_Upload::upload('vignette', array('jpg', 'png', 'gif'));
    	        $data['vignette'] = $result['file'];
	        } catch (Exception $e) {
	            $flashMessenger = $this->getHelper('flashMessenger');
	            $flashMessenger->setNamespace('error')->addMessage($e->getMessage() . ' for Vignette');
	            $error = true;
	        }
	    }
	    if ($_FILES['fichier']['tmp_name']) {
	        try {
	            $result = Admin_Model_Upload::upload('fichier');
	            $data['fichier'] = $result['file'];
	        } catch (Exception $e) {
	            $flashMessenger = $this->getHelper('flashMessenger');
	            $flashMessenger->setNamespace('error')->addMessage($e->getMessage() . ' for Fichier');
	            $error = true;
	        }
	    }

	    if (!$error) {
	        $this->presseModel->save($data, $id);
	        $this->_redirect('admin/presse');
	    }
	    $this->_redirect('admin/presse/edit/id/' . $id);
	}

	public function savePresseAction()
	{
	    $post = $this->getRequest()->getPost();

	    $data = array(
            'titre'      => $post['titre'],
            'resume'     => addslashes($post['resume']),
            'contenu'    => addslashes($post['contenu']),
            'dtcreat'    => date('Y-m-d'),
            'soustitre'  => addslashes($post['soustitre'])
        );

	    if($post['dtpubli']) {
	        $dtpubli = explode('/', $post['dtpubli']);
            $data['dtpubli'] = date("Y-m-d", mktime(0,0,0,$dtpubli[1], $dtpubli[0], $dtpubli[2]));
        }

        $presseModel = new Admin_Model_Presse();
        $num_presse = $presseModel->insert($data);

        $upload_dir = APPLICATION_PATH . '/../public/presse/';
    	if ($_FILES['userfile']['tmp_name'] != "") {
    		$file = $_FILES['userfile'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];
    		$nom_pub = $num_presse .".".$extension;
    		//echo "fichier : " . $nom_pub."<br>";
    		if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir.$nom_pub)) {
    		    $presseModel->update(array('fichier' => $nom_pub), 'p_presse=' . $num_presse);
    		}
    	}
    	if ($_FILES['userfile0']['tmp_name'] != "") {
    		$file = $_FILES['userfile0'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];
    		$nom_pub = $num_presse."_thumb.".$extension;
    		//echo "vignette : " . $nom_pub;
    		if (move_uploaded_file($_FILES['userfile0']['tmp_name'], $upload_dir.$nom_pub)) {
    		    $presseModel->update(array('vignette' => $nom_pub), 'p_presse=' . $num_presse);
    		}
    	}

    	$this->_redirect('admin/presse');
	}

	public function updatePresseAction()
	{
	    $post = $this->getRequest()->getPost();

	    $data = array(
            'titre'      => addslashes($post['titre']),
            'resume'     => addslashes($post['resume']),
            'contenu'    => addslashes($post['contenu']),
            'dtcreat'    => date('Y-m-d'),
            'soustitre'  => addslashes($post['soustitre']),
	        'num_temp'   => md5(uniqid(rand(), true))
        );

	    if($post['dtpubli']) {
            $dtpubli = explode('/', $post['dtpubli']);
            $data['dtpubli'] = date("Y-m-d", mktime(0,0,0,$dtpubli[1], $dtpubli[0], $dtpubli[2]));
        }

        $num_presse  = $post['p_presse'];

        $presseModel = new Admin_Model_Presse();
        $num_presse = $presseModel->update($data, 'p_presse = ' . $num_presse);

        $upload_dir = APPLICATION_PATH . '/../public/presse/';
    	// Si on ne veut pas conserver le fichier, on le delete
    	if ($post[conserve] != "") {
    		if ($post[conserve] != 1) {
    			$fichier = $upload_dir . $post[conserve];
    			unlink($fichier);
    			$presseModel->update(array('fichier' => ''), 'p_presse=' . $num_presse);
    		}
    	}


    	// Insertion d'un nouveau fichier
    	if ($_FILES['userfile']['tmp_name'] != "") {
    		$file = $_FILES['userfile'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];
    		$nom_pub = $num_presse .".".$extension;
    		if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir.$nom_pub)) {
    			if ($post[conserve] == 1) {
    			    $row = $presseModel->getFichier($num_presse);
    				$fichier = $upload_dir . $row['fichier'];
    				unlink($fichier);
    			}

    			$presseModel->update(array('fichier' => $nom_pub), 'p_presse=' . $num_presse);
    		}
    	}
    	// Si on ne veut pas conserver la vignette, on la delete
    	if ($post[conserveVI] != "") {
    		if ($post[conserveVI] != 1) {
    			$fichier = $upload_dir . $_POST[conserveVI];
    			unlink($fichier);
    			$presseModel->update(array('vignette' => ''), 'p_presse=' . $num_presse);
    		}
    	}
    	// Insertion d'une nouvelle vignette
    	if ($_FILES['userfile0']['tmp_name'] != "") {
    		$file = $_FILES['userfile0'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];
    		$nom_pub = $_GET[num] .".".$extension;
    		if (move_uploaded_file($_FILES['userfile0']['tmp_name'], $upload_dir.$nom_pub)) {
    			if ($post[conserveVI] == 1) {
    			    $row = $presseModel->getVignette($num_presse);
    				$vignette = $upload_dir . $row['vignette'];
    				unlink($vignette);
    			}

    			$presseModel->update(array('vignette' => $nom_pub), 'p_presse=' . $num_presse);
    		}
    	}

    	$this->_redirect('admin/presse');
	}

	public function deletePresseAction()
	{
		$id = $this->_getParam('id');
		$page = $this->_getParam('page', 1);
		$presseModel = new Admin_Model_Presse();
		$presseModel->removePresse($id);

		$param = '';
		if($page > 1) $param = 'index/page/' . $page;
		$this->_redirect('admin/presse/' . $param);
		exit;
	}
}

